package com.dingding.user.facade.passenger;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dingding.common.core.beans.UserCoupon;
import com.dingding.facade.user.facade.passanger.coupon.IUserCouponFacade;
import com.dingding.user.core.passenger.coupon.service.IUserCouponService;

/**
 * 
 * @Description: 用户优惠券服务
 * @author CHEHUANBO
 * @date 2016年12月13日
 * @version V2.6.2
 * 
 */
@Component("_userCouponFacade")
public class UserCouponFacadeImpl implements IUserCouponFacade {

	private static final Logger log = Logger.getLogger(UserCouponFacadeImpl.class);

	@Resource(name = "_userCouponService")
	private IUserCouponService userCouponService;

	/**
	 * 
	 * @Description: 添加用户优惠券出现
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	public Map<String, Object> addUserCoupon(Map<String, Object> params) {

		Map<String, Object> resMap = new HashMap<String, Object>();

		try {
			userCouponService.addUserCoupon(params);
			resMap.put("resStatus", true);
			resMap.put("resMsg", "");
		} catch (Exception e) {
			resMap.put("resStatus", true);
			resMap.put("resMsg", "添加用户优惠券服务出现异常！");
			log.error("添加用户优惠券服务出现异常,请求参数：" + params.toString(), e);
		}

		return resMap;

	}

	/**
	 * 
	 * @Description: 获取用户优惠券
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	public Map<String, Object> getUserCoupon(Map<String, Object> params) {

		Map<String, Object> resMap = new HashMap<String, Object>();

		if (params == null) {
			resMap.put("resStatus", false);
			resMap.put("errorMsg", "参数不合法！");
			log.error("调用获取用户优惠券服务被拒绝！原因参数不合法!");

			return resMap;
		}

		String userId = (String) params.get("userId");
		int produceType = (int) params.get("produceType");
		String cityNo = (String) params.get("cityNo");
		int isLock = (int) params.get("isLock"); 

		if (StringUtils.isBlank(userId) || produceType == 0) {
			resMap.put("resStatus", false);
			resMap.put("errorMsg", "参数不合法！" + params.toString());
			log.error("调用获取用户优惠券服务被拒绝！原因参数不合法：" + params.toString());
			return resMap;
		}

		try {

			UserCoupon userCoupon = userCouponService.getUserCoupon(userId, produceType, cityNo);
			
			if(userCoupon!=null && isLock == 2){ //获取用户优惠券同时，并锁死优惠券
				userCouponService.lockUserCouponByCouponId(userCoupon.getUcId(), 3);
			}
			
			resMap.put("resStatus", true);
			resMap.put("userCoupon", userCoupon);

		} catch (Exception e) {
			resMap.put("resStatus", false);
			resMap.put("errorMsg", "服务异常！" + params.toString());
			log.error("调用获取用户优惠券业务出现异常！参数：" + params.toString(), e);
		}

		return resMap;

	}

	/**
	 * 
	 * @Description: 修改用户优惠券状态
	 * @param couponId
	 *            用户优惠券ID
	 * @param updateType
	 *            修改类型 2:启用 3：使用 4：锁定
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	public Map<String, Object> updateUserCouponStatus(Map<String, Object> params) {

		Map<String, Object> resMap = new HashMap<String, Object>();

		if (params == null) {
			resMap.put("resStatus", false);
			resMap.put("errorMsg", "参数不合法！");
			log.error("调用修改用户优惠券状态服务被拒绝！原因参数不合法!");

			return resMap;
		}

		String userCouponId = (String) params.get("couponId");
		int updateType = (int) params.get("updateType");
		if (StringUtils.isBlank(userCouponId) || updateType == 0) {
			resMap.put("resStatus", false);
			resMap.put("errorMsg", "参数不合法！" + params.toString());
			log.error("调用修改用户优惠券状态服务被拒绝！原因参数不合法：" + params.toString());
			return resMap;
		}

		try {
			
			

			userCouponService.lockUserCouponByCouponId(userCouponId, updateType);
			resMap.put("resStatus", true);
			resMap.put("errorMsg", "");

		} catch (Exception e) {
			resMap.put("resStatus", false);
			resMap.put("errorMsg", "服务器异常！");
			log.error("调用修改用户优惠券状态服务出现异常！", e);
		}

		return resMap;

	}

}
